package com.lin.gobang;

import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lin.db.GameDB;

/**
 * Created by x1 on 2016/5/19.
 * 注册界面的Activity
 */
public class Sign extends Activity {
    private Button ok;
    private EditText user;
    private EditText password1;
    private EditText password;
    private SQLiteDatabase dbRead,dbWrite;
    private GameDB db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        user= (EditText) findViewById(R.id.user);
        password1= (EditText) findViewById(R.id.password);
        password= (EditText) findViewById(R.id.password2);

        //database initial
        db = new GameDB(this);
        dbRead = db.getReadableDatabase();
        dbWrite = db.getWritableDatabase();
        ok= (Button) findViewById(R.id.sure);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=user.getText().toString();
                String passw1=password1.getText().toString();
                String passw=password.getText().toString();
                if(passw.trim().equals("")||passw1.trim().equals(""))
                {
                    String text1="输入的密码不能为空，请重新输入";
                    Toast toasts=Toast.makeText(Sign.this,text1,Toast.LENGTH_SHORT);
                    toasts.show();
                }
                if(passw.equals(passw1))
                {
                    try{

                    }catch (Exception e)
                    {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),"注册失败!",Toast.LENGTH_SHORT).show();
                    }
                    String text = "";
                    if(saveUser()!=-1){
                    text="注册成功，请返回登录";

                    }else{
                       text = "注册失败！！！This user have been existed!";
                    }
                    Toast toastt=Toast.makeText(Sign.this,text,Toast.LENGTH_SHORT);
                    toastt.show();


                }
                else
                {
                    String text="两次密码输入不相同，请重新输入";
                    Toast toast=Toast.makeText(Sign.this,text,Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }
    //insert user in database
    public int saveUser(){
        ContentValues cv = new ContentValues();
        cv.put("username",user.getText().toString());
        cv.put("password",password.getText().toString());
        cv.put("rank",0);
        cv.put("score",0);
        return (int)dbWrite.insert("Users",null,cv);
    }

    @Override
    protected void onDestroy() {
        dbRead.close();
        dbWrite.close();
        super.onDestroy();
    }
}
